package com.aaa.service.impl;

import com.aaa.dao.CategoryDao;
import com.aaa.dao.impl.CategoryDaoImpl;
import com.aaa.entity.Category;
import com.aaa.service.CategoryService;
import com.aaa.util.BusinessException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CategoryServiceImpl implements CategoryService {
    CategoryDao categoryDao = new CategoryDaoImpl();

    @Override
    public int addCategory(Category category) {
        return categoryDao.addCategory(category);
    }

    @Override
    public int updateCategory(Category category) {
        return categoryDao.updateCategory(category);
    }

    @Override
    public int changeStatus(Category category) {
        return categoryDao.changeStatus(category);
    }

    @Override
    public List<Category> getCategoryList() {
        return categoryDao.getCategoryList();
    }

    @Override
    public Map<String, Object> getAllCategoryList(Integer pageNumber, Integer pageSize, String searchStuno, String searchName) throws Exception {
        if (pageNumber == null || pageNumber == 0) {
            throw new BusinessException("当前页数不能为空");
        }
        if (pageSize == null || pageSize == 0) {
            throw new BusinessException("每页条数不能为空");
        }
        pageNumber = (pageNumber - 1) * pageSize;
        // 当前页码数据
        List<Category> list = categoryDao.getAllCategoryList(pageNumber, pageSize, searchStuno, searchName);

        // 总页数
        int count = categoryDao.getAllCategoryInfoCount(searchStuno, searchName);

        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return map;
    }
}
